import { GET_AUTH_INFO } from './actionName'

import { getAuthInfo } from '@/services/sapi'

const defaultValue = {
  token: '',
  expires_time: 0,
  accessMenus: [],
}

/**
 * 用于登陆的action
 * @param {*} options
 * @returns
 */
export function tryLogin(options) {
  return {
    type: GET_AUTH_INFO,
    payload: getAuthInfo(options)
      .then(res => {
        if (res.status === 200) {
          return {
            token: res.data.token,
            expires_time: res.data.expires_time,
            accessMenus: res.data.menus,
          }
        }
      })
      .catch(err => {
        console.log(err)
      }),
  }
}

export default function authInfo(state = defaultValue, { type, payload }) {
  switch (type) {
    case GET_AUTH_INFO:
      return { ...state, ...payload }
    default:
      return state
  }
}
